// 自定义弹框
export const Toast = (msg, duration, url) =>{
    duration = isNaN(duration) ? 3000 : duration;
    var m = document.createElement('div');
    m.innerHTML = msg;
    m.style.cssText = "width:14%; min-width:150px; background:rgba(0,0,0,0.7); padding:0px 2%x;opacity:0.8; height:40px; color:#fff; line-height:40px; text-align:center; border-radius:5px; position:fixed; top:30%; left:50%;margin-left:-75px; z-index:99999999;";
    document.body.appendChild(m);
    setTimeout(function() {
        var d = 0.5;
        m.style.webkitTransition = '-webkit-transform ' + d
            + 's ease-in, opacity ' + d + 's ease-in';
        m.style.opacity = '0';
        setTimeout(function() {
            document.body.removeChild(m)
        }, d * 1000);
        if (url != '') {
            if (url == 'reload') {
                window.location.reload();
            } else {
                window.location.href = getRootPath() + url;
            }
        }
    }, duration);

};

function getRootPath() {
    var strFullPath = window.document.location.href;
    var strPath = window.document.location.pathname;
    var pos = strFullPath.indexOf(strPath);
    var prePath = strFullPath.substring(0, pos);
    var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    return (prePath + postPath);
    // return prePath ;
}

export const checkLogin =(obj,flag,errorUrl) =>{

    if(obj.head.subCode==4100)
    {
        location.href='#/mall/login';
        return false;
    }
    if(obj.head.subCode != '0')

    {
        Toast(obj.head.subMsg, 2000, errorUrl);
        return false;
    }
    if(flag)
    {
        Toast(obj.head.subMsg, 2000,'');
    }
    return true;
};


function HashMap() {
    this.map = {};
}


HashMap.prototype = {
    put : function(key, value) {
        this.map[key] = value;
    },
    get : function(key) {
        if (this.map.hasOwnProperty(key)) {
            return this.map[key];
        }
        return null;
    },
    remove : function(key) {
        if (this.map.hasOwnProperty(key)) {
            return delete this.map[key];
        }
        return false;
    },
    removeAll : function() {
        this.map = {};
    },
    keySet : function() {
        var _keys = [];
        for ( var i in this.map) {
            _keys.push(i);
        }
        return _keys;
    }
};


HashMap.prototype.constructor = HashMap;


Date.prototype.format = function(format) {
    var o = {
        "M+" : this.getMonth() + 1,
        "d+" : this.getDate(),
        "h+" : this.getHours(),
        "m+" : this.getMinutes(),
        "s+" : this.getSeconds(),
        "q+" : Math.floor((this.getMonth() + 3) / 3),
        "S" : this.getMilliseconds()
    };
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    }
    for ( var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
                : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};

function getSmpFormatDate(date, isFull) {
    var pattern = "";
    if (isFull == true || isFull == undefined) {
        pattern = "yyyy/MM/dd";
    } else {
        pattern = "yyyy/MM/dd";
    }
    return getFormatDate(date, pattern);
}

function getSmpFormatNowDate(isFull) {
    return getSmpFormatDate(new Date(), isFull);
}

function getSmpFormatDateByLong(l, isFull) {
    return getSmpFormatDate(new Date(l), isFull);
}

export const getFormatDateByLong =(l, pattern) =>
 {
    return getFormatDate(new Date(l), pattern);
};

function getFormatDate(date, pattern) {
    if (date == undefined) {
        date = new Date();
    }
    if (pattern == undefined) {
        pattern = "yyyy/MM/dd";
    }
    return date.format(pattern);
}



export const initFunction =() =>{

    FastClick.attach(document.body);


    var a = document.getElementsByTagName('a');
    for(var i = 0; i < a.length; i++)
    {
        a[i].addEventListener('touchstart',function(){},false);
    }

}

export const fade =() =>{
    //页面淡入淡出
    $(document).ready(function(){

        $(".fadeaction ").css("display","none");

        $(".fadeaction").fadeIn();

        $("a[target],a[href*='javascript'],a.lightbox-processed,a[href*='#']").addClass("speciallinks");
        $("a:not(.speciallinks)").click(function(){
            $(".fadeaction").fadeOut();
            $("object,embed").css("visibility","hidden");
        });
    });
}

